/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cdp.web.component.metadata.serializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.cdp.web.component.metadata.define.WebComponentMetadata;
import com.inspur.edp.cdp.web.component.metadata.serializer.obsolete.Serializer;
import com.inspur.edp.cdp.web.component.metadata.serializer.obsolete.WebComp;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class SerializerTest {

    @Test
    public void oldDataSerializeTest() throws IOException {
        File fileDirectory = new File("./src/test/data");
        WebComponentMetadataSerializer serializer = new WebComponentMetadataSerializer();
        Serializer oldSerializer = new Serializer();
        for (File file : fileDirectory.listFiles()) {
            String componentStr = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
            JsonNode metadataNode = (new ObjectMapper()).readTree(componentStr);
            JsonNode compNode = JsonUtil.getPropValue(metadataNode, "Content", JsonNode.class);

            WebComponentMetadata newComponent = (WebComponentMetadata) serializer.DeSerialize(compNode);
            WebComp oldComponent = (WebComp) oldSerializer.DeSerialize(compNode);
            Assert.assertTrue(CompareUtil.differentObjectEquals(oldComponent, newComponent));
        }
    }

    @Test
    public void newDataSerializeTest() {
        WebComponentMetadata component = ComponentInit.get();
        WebComponentMetadataSerializer serializer = new WebComponentMetadataSerializer();
        JsonNode compNode1 = serializer.Serialize(component);
        WebComponentMetadata component2 = (WebComponentMetadata) serializer.DeSerialize(compNode1);
        JsonNode compNode2 = serializer.Serialize(component2);

        Assert.assertEquals(compNode2.toString(), compNode1.toString());
        Assert.assertTrue(CompareUtil.equals(component, component2));
    }

    @Test
    public void oldDataDeSerializeTest() throws IOException {
        File fileDirectory = new File("./src/test/data");
        JsonSerializer serializer = new JsonSerializer();
        for (File file : fileDirectory.listFiles()) {
            String componentStr = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
            JsonNode metadataNode = (new ObjectMapper()).readTree(componentStr);
            JsonNode compNode = JsonUtil.getPropValue(metadataNode, "Content", JsonNode.class);

            WebComponentMetadata newComponent = (WebComponentMetadata) serializer.DeSerialize(compNode);
            JsonNode node = serializer.Serialize(newComponent);
            WebComponentMetadata newComponent2 = (WebComponentMetadata) serializer.DeSerialize(node);
            Assert.assertTrue(CompareUtil.equals(newComponent2, newComponent));
        }
    }
}
